{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Max Difference You Can Get From Changing an Integer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maxDiff"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #改变一个整数能得到的最大差值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数&nbsp;<code>num</code>&nbsp;。你可以对它进行如下步骤恰好 <strong>两次</strong>&nbsp;：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>选择一个数字&nbsp;<code>x (0&nbsp;&lt;= x &lt;= 9)</code>.</li>\n",
    "\t<li>选择另一个数字&nbsp;<code>y (0&nbsp;&lt;= y &lt;= 9)</code>&nbsp;。数字&nbsp;<code>y</code>&nbsp;可以等于&nbsp;<code>x</code>&nbsp;。</li>\n",
    "\t<li>将 <code>num</code>&nbsp;中所有出现 <code>x</code>&nbsp;的数位都用 <code>y</code>&nbsp;替换。</li>\n",
    "\t<li>得到的新的整数 <strong>不能</strong>&nbsp;有前导 0 ，得到的新整数也 <strong>不能</strong>&nbsp;是 0&nbsp;。</li>\n",
    "</ul>\n",
    "\n",
    "<p>令两次对 <code>num</code>&nbsp;的操作得到的结果分别为&nbsp;<code>a</code>&nbsp;和&nbsp;<code>b</code>&nbsp;。</p>\n",
    "\n",
    "<p>请你返回&nbsp;<code>a</code> 和&nbsp;<code>b</code>&nbsp;的 <strong>最大差值</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 555\n",
    "<strong>输出：</strong>888\n",
    "<strong>解释：</strong>第一次选择 x = 5 且 y = 9 ，并把得到的新数字保存在 a 中。\n",
    "第二次选择 x = 5 且 y = 1 ，并把得到的新数字保存在 b 中。\n",
    "现在，我们有 a = 999 和 b = 111 ，最大差值为 888\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 9\n",
    "<strong>输出：</strong>8\n",
    "<strong>解释：</strong>第一次选择 x = 9 且 y = 9 ，并把得到的新数字保存在 a 中。\n",
    "第二次选择 x = 9 且 y = 1 ，并把得到的新数字保存在 b 中。\n",
    "现在，我们有 a = 9 和 b = 1 ，最大差值为 8\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 123456\n",
    "<strong>输出：</strong>820000\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 10000\n",
    "<strong>输出：</strong>80000\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 5：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 9288\n",
    "<strong>输出：</strong>8700\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num &lt;= 10^8</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [max-difference-you-can-get-from-changing-an-integer](https://leetcode.cn/problems/max-difference-you-can-get-from-changing-an-integer/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [max-difference-you-can-get-from-changing-an-integer](https://leetcode.cn/problems/max-difference-you-can-get-from-changing-an-integer/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['555', '9']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:\n",
    "        n = 0\n",
    "        minVal, maxVal = 0, 0\n",
    "        for i in range(len(differences)):\n",
    "            n += differences[i]\n",
    "            minVal = min(minVal, n)\n",
    "            maxVal = max(maxVal, n)\n",
    "\n",
    "        maxDiff = maxVal - minVal\n",
    "        ans = (upper - lower) - maxDiff + 1\n",
    "        return max(0, ans) # 如果还原后的数组跨度大于upper-lower，则没有有效解\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:\n",
    "        n = 0\n",
    "        minVal, maxVal = 0, 0\n",
    "        for i in range(len(differences)):\n",
    "            n += differences[i]\n",
    "            minVal = min(minVal, n)\n",
    "            maxVal = max(maxVal, n)\n",
    "\n",
    "        maxDiff = maxVal - minVal\n",
    "        ans = (upper - lower) - maxDiff + 1\n",
    "        return max(0, ans) # 如果还原后的数组跨度大于upper-lower，则没有有效解\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:\n",
    "        n = 0\n",
    "        minVal, maxVal = 0, 0\n",
    "        for i in range(len(differences)):\n",
    "            n += differences[i]\n",
    "            minVal = min(minVal, n)\n",
    "            maxVal = max(maxVal, n)\n",
    "\n",
    "        maxDiff = maxVal - minVal\n",
    "        ans = (upper - lower) - maxDiff + 1\n",
    "        return max(0, ans) # 如果还原后的数组跨度大于upper-lower，则没有有效解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        min_num, max_num = str(num), str(num)\n",
    "\n",
    "        # 找到一个高位替换成 9\n",
    "        for digit in max_num:\n",
    "            if digit != \"9\":\n",
    "                max_num = max_num.replace(digit, \"9\")\n",
    "                break\n",
    "\n",
    "        # 将最高位替换成 1\n",
    "        # 或者找到一个与最高位不相等的高位替换成 0\n",
    "        for i, digit in enumerate(min_num):\n",
    "            if i == 0:\n",
    "                if digit != \"1\":\n",
    "                    min_num = min_num.replace(digit, \"1\")\n",
    "                    break\n",
    "            else:\n",
    "                if digit != \"0\" and digit != min_num[0]:\n",
    "                    min_num = min_num.replace(digit, \"0\")\n",
    "                    break\n",
    "\n",
    "        return int(max_num) - int(min_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        num = list(num)\n",
    "        if len(num)==1:\n",
    "            return 8\n",
    "        a = num[:]\n",
    "        if num[0]=='9':\n",
    "            flag = '>'\n",
    "            for i in range(len(num)):\n",
    "                if num[i]!='9':\n",
    "                    flag = num[i]\n",
    "                    break\n",
    "            for i in range(len(num)):\n",
    "                if a[i]==flag:\n",
    "                    a[i] = '9'\n",
    "        else:\n",
    "            flag = num[0]\n",
    "            for i in range(len(num)):\n",
    "                if a[i]==flag:\n",
    "                    a[i] = '9'\n",
    "        a = int(''.join(a))\n",
    "        b = num[:]\n",
    "        if num[0]=='1':\n",
    "            flag = '>'\n",
    "            for i in range(len(num)):\n",
    "                if num[i]!='1' and num[i] !='0':\n",
    "                    flag = num[i]\n",
    "                    break\n",
    "            for i in range(len(num)):\n",
    "                if num[i]==flag:\n",
    "                    b[i] = '0' \n",
    "        else:\n",
    "            flag = b[0]\n",
    "            for i in range(len(num)):\n",
    "                if num[i]==flag:\n",
    "                    b[i] = '1'\n",
    "        b = int(''.join(b))\n",
    "        return a-b\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        def change(x, y):\n",
    "            return str(num).replace(str(x), str(y))\n",
    "        mn = mx = num\n",
    "        for x in range(10):\n",
    "            for y in range(10):\n",
    "                res = change(x, y)\n",
    "                if res[0] != '0':\n",
    "                    res = int(res)\n",
    "                    mn = min(mn, res)\n",
    "                    mx = max(mx, res)\n",
    "        return mx - mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        a, b = str(num), str(num)\n",
    "        for x in a:\n",
    "            if x != '9':\n",
    "                a = a.replace(x, '9')\n",
    "                break\n",
    "        for j, y in enumerate(b):\n",
    "            if j == 0:\n",
    "                if y != '1':\n",
    "                    b = b.replace(y, '1')\n",
    "                    break\n",
    "            else:\n",
    "                if y != '0' and y != b[0]:\n",
    "                    b = b.replace(y, '0')\n",
    "                    break\n",
    "        return int(a) - int(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        if num<10:\n",
    "            return 8\n",
    "        numStr = str(num)\n",
    "        a,b = 10**(len(numStr))-1,num\n",
    "        for i in range(len(numStr)):\n",
    "            if numStr[i] != '9':\n",
    "                a = int(numStr.replace(numStr[i],'9'))\n",
    "                break\n",
    "        \n",
    "        for i in range(0,len(numStr)):\n",
    "            if i==0 and numStr[i] != '1':\n",
    "                b =  int(numStr.replace(numStr[i],'1'))\n",
    "                break\n",
    "            elif i>0 and numStr[i] != '0' and numStr[i] != numStr[0]:\n",
    "                b = int(numStr[0]+numStr[1:].replace(numStr[i],'0'))\n",
    "                break\n",
    "\n",
    "        return a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num_max = num_min = str(num)\n",
    "\n",
    "        # 计算替换后的最大值\n",
    "        for ch in num_max:\n",
    "            if ch != \"9\":\n",
    "                num_max = num_max.replace(ch, \"9\")\n",
    "                break\n",
    "\n",
    "        # 计算替换后的最小值\n",
    "        first_num = num_min[0]\n",
    "        for i, ch in enumerate(num_min):\n",
    "            if i == 0:\n",
    "                if ch != \"1\":\n",
    "                    num_min = num_min.replace(ch, \"1\")\n",
    "                    break\n",
    "            elif ch != \"0\" and ch != first_num:\n",
    "                num_min = num_min.replace(ch, \"0\")\n",
    "                break\n",
    "\n",
    "        return int(num_max) - int(num_min)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        mx_str, mi_str = str(num), str(num)\n",
    "\n",
    "        for i in mx_str:\n",
    "            if i != '9':\n",
    "                mx_str = mx_str.replace(i, '9')\n",
    "                break\n",
    "\n",
    "        for i, diff in enumerate(mi_str):\n",
    "            if i == 0:\n",
    "                if diff != '1':\n",
    "                    mi_str = mi_str.replace(diff, '1')\n",
    "                    break\n",
    "            else:\n",
    "                if diff != '0' and diff != mi_str[0]:\n",
    "                    mi_str = mi_str.replace(diff, '0')\n",
    "                    break\n",
    "\n",
    "        return int(mx_str) - int(mi_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        min_num, max_num = str(num), str(num)\n",
    "\n",
    "        # 找到一个高位替换成 9\n",
    "        for digit in max_num:\n",
    "            if digit != \"9\":\n",
    "                max_num = max_num.replace(digit, \"9\")\n",
    "                break\n",
    "\n",
    "\n",
    "        for i, digit in enumerate(min_num):\n",
    "            if i == 0:\n",
    "                if digit != \"1\":\n",
    "                   min_num = min_num.replace(digit, \"1\")\n",
    "                   break\n",
    "            else:\n",
    "                if digit != '0' and digit != min_num[0]:\n",
    "                    min_num = min_num.replace(digit, '0')\n",
    "                    break\n",
    "        return int(max_num) - int(min_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num1=num2=num=str(num)\n",
    "        for char in num:\n",
    "            if char!='9':\n",
    "                num1=num.replace(char,'9')\n",
    "                break\n",
    "        for index,char in enumerate(num):\n",
    "            if char!='1' and index==0:\n",
    "                num2=num.replace(char,'1')\n",
    "                break\n",
    "            if char!='0' and index!=0 and char!=num[0]:\n",
    "                num2=num.replace(char,'0')\n",
    "                break\n",
    "        # print(num1,num2)\n",
    "        return int(num1)-int(num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        mn = mx = num\n",
    "        # 最小值：如果第一个数不为1，替换它为1，否则替换第二个以后的第一个非0数为0，这个数还不能等于第一个数\n",
    "        # 如果第二个以后没有非0数，什么也不做\n",
    "        start = num[0]\n",
    "        if start != '1':\n",
    "            mn = num.replace(start, '1')\n",
    "        else:\n",
    "            for c in num[1:]:\n",
    "                if c != '0' and c != start:\n",
    "                    mn = num.replace(c, '0')\n",
    "                    break\n",
    "        # 最大值:将第一个非9数替换为9\n",
    "        for c in num:\n",
    "            if c != '9':\n",
    "                mx = num.replace(c, '9')\n",
    "                break\n",
    "        return int(mx) - int(mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        s1, s2 = s, s\n",
    "        for x in s:\n",
    "            if x != '9':\n",
    "                s1 = s1.replace(x, '9')\n",
    "                break\n",
    "\n",
    "        if s[0] != '1':\n",
    "            s2 = s2.replace(s[0], '1')\n",
    "        else:\n",
    "            for x in s:\n",
    "                if x != '1' and x != s[0] and x != '0':\n",
    "                    s2 = s2.replace(x, '0')\n",
    "                    break\n",
    "        print(s1, s2)\n",
    "        return int(s1) - int(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def get_min_num(self, num: int) -> int:\n",
    "        str_num = str(num)\n",
    "        min_taget = None\n",
    "        first_num = str_num[0]\n",
    "        if first_num != \"1\":\n",
    "            return int(str_num.replace(first_num, \"1\"))\n",
    "\n",
    "        for i, c in enumerate(str_num):\n",
    "            if i != 0 and c != \"0\" and c != first_num:\n",
    "                min_taget = (c, \"0\")\n",
    "                break\n",
    "        min_num = num\n",
    "        if min_taget is not None:\n",
    "            min_num = int(str_num.replace(min_taget[0], min_taget[1]))\n",
    "        return min_num\n",
    "\n",
    "    def get_max_num(self, num: int) -> int:\n",
    "        str_num = str(num)\n",
    "        max_taget = None\n",
    "        for i, c in enumerate(str_num):\n",
    "            if c != \"9\":\n",
    "                max_taget = (c, \"9\")\n",
    "                break\n",
    "        max_num = num\n",
    "        if max_taget is not None:\n",
    "            max_num = int(str_num.replace(max_taget[0], max_taget[1]))\n",
    "        return max_num\n",
    "\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        return self.get_max_num(num) - self.get_min_num(num)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        big, small, bigInit, smallInit = '', '', '', ''\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                bigInit = c\n",
    "                break\n",
    "        if s[0] == '1':\n",
    "            for c in s[1:]:\n",
    "                if c != '0' and c != '1':\n",
    "                    smallInit = c\n",
    "                    break\n",
    "        else:\n",
    "            smallInit = s[0]\n",
    "\n",
    "        for c in s:\n",
    "            big += '9' if c == bigInit else c\n",
    "            small += ('0' if s[0] == '1' else '1') if c == smallInit else c\n",
    "        \n",
    "        print(bigInit, big, smallInit, small)\n",
    "        \n",
    "        return int(big) - int(small)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        numStr = str(num)\n",
    "        maxNum = ''\n",
    "        cur = None\n",
    "        for i in numStr:\n",
    "            if i != '9':\n",
    "                if cur:\n",
    "                    if i == cur:\n",
    "                        maxNum += '9'\n",
    "                    else:\n",
    "                        maxNum += i\n",
    "                else:\n",
    "                    cur = i\n",
    "                    maxNum += '9'\n",
    "            else:\n",
    "                maxNum += '9'\n",
    "        minNum = ''\n",
    "        cur = None\n",
    "        chang = None\n",
    "        for k, i in enumerate(numStr):\n",
    "            if k == 0:\n",
    "                if i != '1':\n",
    "                    cur = i\n",
    "                    chang = '1'\n",
    "                else:\n",
    "                    chang = '0'\n",
    "                minNum += '1'\n",
    "            else:\n",
    "                if cur:\n",
    "                    if i == cur:\n",
    "                        minNum += chang\n",
    "                    else:\n",
    "                        minNum += i\n",
    "                else:\n",
    "                    if i != '1' and i != '0':\n",
    "                        cur = i\n",
    "                        chang = '0'\n",
    "                        minNum += chang\n",
    "                    else:\n",
    "                        minNum += i\n",
    "        return int(maxNum) - int(minNum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        min_num, max_num = str(num), str(num)\n",
    "\n",
    "        # 找到一个高位替换成 9\n",
    "        for digit in max_num:\n",
    "            if digit != \"9\":\n",
    "                max_num = max_num.replace(digit, \"9\")\n",
    "                break\n",
    "\n",
    "        # 将最高位替换成 1\n",
    "        # 或者找到一个与最高位不相等的高位替换成 0\n",
    "        for i, digit in enumerate(min_num):\n",
    "            if i == 0:\n",
    "                if digit != \"1\":\n",
    "                    min_num = min_num.replace(digit, \"1\")\n",
    "                    break\n",
    "            else:\n",
    "                if digit != \"0\" and digit != min_num[0]:\n",
    "                    min_num = min_num.replace(digit, \"0\")\n",
    "                    break\n",
    "\n",
    "        return int(max_num) - int(min_num)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        def helper(x):\n",
    "            return all(i == x[0] for i in x)\n",
    "        if helper(str(num)):\n",
    "            n = len(str(num))\n",
    "            return int('8'*n)\n",
    "        choose = str(num)[0]\n",
    "        for i in str(num):\n",
    "            if i != '9':\n",
    "                choose = i\n",
    "                break\n",
    "        a = str(num).replace(choose, '9')\n",
    "        a = int(a)\n",
    "        if str(num)[0] == '1':\n",
    "            choose = str(num)[1]\n",
    "            for i in str(num)[1:]:\n",
    "                if i != '0' and i != '1':\n",
    "                    choose = i\n",
    "                    break\n",
    "            b = str(num).replace(choose, '0')\n",
    "            b = int(b)\n",
    "        else:\n",
    "            choose = str(num)[0]\n",
    "            b = str(num).replace(choose, '1')\n",
    "            b = int(b)\n",
    "        return a - b\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        for i in s:\n",
    "            if i != '9':\n",
    "                max = s.replace(i, '9')\n",
    "                break\n",
    "        else:\n",
    "            max = s\n",
    "        if s[0] != '1':\n",
    "            min = s.replace(s[0], '1')\n",
    "        else:\n",
    "            for i in s:\n",
    "                if i != '1' and i != '0':\n",
    "                    min = s.replace(i, '0')\n",
    "                    break\n",
    "            else:\n",
    "                min = s\n",
    "        return int(max)-int(min)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num_str = str(num)\n",
    "        # Find the max a\n",
    "        a = num\n",
    "        for i in range(len(num_str)):\n",
    "            if int(num_str[i])<9:\n",
    "                a = int(num_str.replace(num_str[i], '9'))\n",
    "                break\n",
    "\n",
    "        # Find the min b\n",
    "        b = num\n",
    "        for i in range(len(num_str)):\n",
    "            if i==0 and int(num_str[i])>1:\n",
    "                b = int(num_str.replace(num_str[i], '1'))\n",
    "                break\n",
    "            elif i>0 and int(num_str[i])>0:\n",
    "                if num_str[i] != num_str[0]:\n",
    "                    b = int(num_str.replace(num_str[i], '0'))\n",
    "                    break\n",
    "                elif int(num_str[i])>1:\n",
    "                    b = int(num_str.replace(num_str[i], '1'))\n",
    "                    break\n",
    "\n",
    "        # Calculate a-b\n",
    "        return a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num_str = str(num)\n",
    "        first_replace = '9'\n",
    "        for ch in num_str:\n",
    "            if ch != '9':\n",
    "                first_replace = ch\n",
    "                break\n",
    "        first_num_str = num_str.replace(first_replace, '9')\n",
    "\n",
    "        second_num_str = num_str\n",
    "        for index, ch in enumerate(num_str):\n",
    "            if (num_str[:1] == \"1\" and ch == '1') or ch == '0':\n",
    "                continue\n",
    "\n",
    "            if index == 0:\n",
    "                second_num_str = num_str.replace(ch, '1')\n",
    "            else:\n",
    "                second_num_str = num_str.replace(ch, '0')\n",
    "            break\n",
    "\n",
    "        if int(second_num_str) == 0:\n",
    "            second_num_str = num_str\n",
    "        return int(first_num_str) - int(second_num_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        numa = [e for e in str(num)]\n",
    "        numb = numa[:]\n",
    "        n = len(numa)\n",
    "\n",
    "        cdigitA = 'a'\n",
    "        for i in range(n):\n",
    "            if numa[i]<'9':\n",
    "                cdigitA = numa[i]\n",
    "                break\n",
    "        if cdigitA!='a':\n",
    "            for i in range(n):\n",
    "                if numa[i] == cdigitA:\n",
    "                    numa[i] = '9'\n",
    "\n",
    "        cdigitB = 'a'\n",
    "        if numb[0] != '1':\n",
    "            cdigitB = numb[0]\n",
    "            for i in range(n):\n",
    "                if numb[i] == cdigitB:\n",
    "                    numb[i] = '1'\n",
    "        else:\n",
    "            for i in range(1,n):\n",
    "                if numb[i]>'1':\n",
    "                    cdigitB = numb[i]\n",
    "                    break\n",
    "            if cdigitB != 'a':\n",
    "                for i in range(n):\n",
    "                    if numb[i] == cdigitB:\n",
    "                        numb[i] = '0'\n",
    "\n",
    "        \n",
    "        max_v = int(''.join(numa))\n",
    "        min_v = int(''.join(numb))\n",
    "        return max_v - min_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        nums = list(str(num))\n",
    "        x = -1\n",
    "        if nums[0] == '9':\n",
    "            for i in range(1, len(nums)):\n",
    "                if nums[i] != '9':\n",
    "                    x = nums[i]\n",
    "                    break\n",
    "            if x != -1:\n",
    "                for i in range(1, len(nums)):\n",
    "                    if nums[i] == x:\n",
    "                        nums[i] = '9'\n",
    "        else:\n",
    "            x = nums[0]\n",
    "            for i in range(len(nums)):\n",
    "                if nums[i] == x:\n",
    "                    nums[i] = '9'\n",
    "        a = int(''.join(nums))\n",
    "        nums = list(str(num))\n",
    "        x = -1\n",
    "        if nums[0] == '1':\n",
    "            for i in range(1, len(nums)):\n",
    "                if nums[i] != '1' and nums[i] != '0':\n",
    "                    x = nums[i]\n",
    "                    break\n",
    "            if x != -1:\n",
    "                for i in range(1, len(nums)):\n",
    "                    if nums[i] == x:\n",
    "                        nums[i] = '0'\n",
    "        else:\n",
    "            x = nums[0]\n",
    "            for i in range(len(nums)):\n",
    "                if nums[i] == x:\n",
    "                    nums[i] = '1'\n",
    "        b = int(''.join(nums))\n",
    "        return a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        max_num, min_num = str(num), str(num)\n",
    "\n",
    "        for digit in max_num:\n",
    "            if digit != \"9\":\n",
    "                max_num = max_num.replace(digit, \"9\")\n",
    "                break\n",
    "        \n",
    "        for i, digit in enumerate(min_num):\n",
    "            if i == 0:\n",
    "                if digit != \"1\":\n",
    "                    min_num = min_num.replace(digit, \"1\")\n",
    "                    break\n",
    "            else:\n",
    "                if digit != \"0\" and digit != \"1\":\n",
    "                    min_num = min_num.replace(digit, \"0\")\n",
    "                    break\n",
    "\n",
    "        return int(max_num) - int(min_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num_str = str(num)\n",
    "        # Find the max a\n",
    "        a = num\n",
    "        for i in range(len(num_str)):\n",
    "            if int(num_str[i])<9:\n",
    "                a = int(num_str.replace(num_str[i], '9'))\n",
    "                break\n",
    "\n",
    "        # Find the min b\n",
    "        b = num\n",
    "        for i in range(len(num_str)):\n",
    "            if i==0 and int(num_str[i])>1:\n",
    "                b = int(num_str.replace(num_str[i], '1'))\n",
    "                break\n",
    "            elif i>0 and int(num_str[i])>0:\n",
    "                if num_str[i] != num_str[0]:\n",
    "                    b = int(num_str.replace(num_str[i], '0'))\n",
    "                    break\n",
    "                elif int(num_str[i])>1:\n",
    "                    b = int(num_str.replace(num_str[i], '1'))\n",
    "                    break\n",
    "\n",
    "        # Calculate a-b\n",
    "        return a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        def change(x, y):\n",
    "            return str(num).replace(str(x), str(y))\n",
    "\n",
    "        mx = mi = num\n",
    "        for x in range(10):\n",
    "            for y in range(10):\n",
    "                res = change(x, y)\n",
    "                if res[0] != '0':\n",
    "                    mi = min(mi, int(res))\n",
    "                    mx = max(mx, int(res))\n",
    "        return mx - mi\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num = list(str(num))\n",
    "\n",
    "        a = num[:]\n",
    "        b = num[:]\n",
    "\n",
    "        index=0\n",
    "        while index <len(a) and a[index]=='9':\n",
    "            index+=1\n",
    "        if index==len(a):\n",
    "            index-=1\n",
    "        val_a=a[index]\n",
    "        a[index]='9'\n",
    "        for i in range(index+1, len(a)):\n",
    "            if a[i] == val_a:\n",
    "                a[i] = a[index]\n",
    "        a = int(\"\".join(a))\n",
    "\n",
    "        val_b = b[0]\n",
    "        if val_b == '1':\n",
    "            index=1\n",
    "            while index<len(b) and (b[index]=='1' or b[index]=='0'):\n",
    "                index+=1\n",
    "            if not index==len(b):\n",
    "\n",
    "                val_b = b[index]\n",
    "                b[index] = '0'\n",
    "                for i in range(index+1, len(b)):\n",
    "                    if b[i] == val_b:\n",
    "                        b[i] = b[index]\n",
    "        else:\n",
    "\n",
    "            b[0] = '1'\n",
    "            for i in range(1, len(b)):\n",
    "                if b[i] == val_b:\n",
    "                    b[i] = b[0]\n",
    "        b = int(\"\".join(b))\n",
    "        return abs(a - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        alist=list(str(num))\n",
    "        b=0\n",
    "        for i in range(len(alist)):\n",
    "            if alist[i]=='9':\n",
    "                continue\n",
    "            else:\n",
    "                ch=alist[i]\n",
    "                for j in range(i,len(alist)):\n",
    "                    if alist[j]==ch:\n",
    "                        alist[j]='9'\n",
    "                break\n",
    "        a=int(''.join(alist))\n",
    "        alist=list(str(num))\n",
    "        cal=0\n",
    "        for i in range(len(alist)):\n",
    "            if alist[i]=='1':\n",
    "                cal=1\n",
    "            elif alist[i]!='0' and cal==1:\n",
    "                ch=alist[i]\n",
    "                for j in range(i,len(alist)):\n",
    "                    if alist[j]==ch:\n",
    "                        alist[j]='0'\n",
    "                break\n",
    "            elif alist[i]!='0' and cal==0:\n",
    "                ch=alist[i]\n",
    "                for j in range(i,len(alist)):\n",
    "                    if alist[j]==ch:\n",
    "                        alist[j]='1'\n",
    "                break\n",
    "        b=int(''.join(alist))\n",
    "        return a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        n = len(num)\n",
    "        max_ = list(num)\n",
    "        r = None\n",
    "        for i in range(n):\n",
    "            if max_[i] != '9' and r is None:\n",
    "                r = max_[i]\n",
    "            if r is not None and max_[i] == r:\n",
    "                max_[i] = '9'\n",
    "        \n",
    "        min_ = list(num)\n",
    "        r = None\n",
    "        if min_[0] == '1':\n",
    "            for i in range(1, n):\n",
    "                if min_[i] != '1' and min_[i] != '0' and r is None:\n",
    "                    r = min_[i]\n",
    "                if r is not None and min_[i] == r:\n",
    "                    min_[i] = '0'\n",
    "        else:\n",
    "            r = min_[0]\n",
    "            for i in range(n):\n",
    "                if min_[i] == r:\n",
    "                    min_[i] = '1'\n",
    "        return int(''.join(max_)) - int(''.join(min_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        def change(x, y):\n",
    "            return str(num).replace(str(x), str(y))\n",
    "\n",
    "        min_n = max_n = num\n",
    "        for i in range(10):\n",
    "            for j in range(10):\n",
    "                res = change(i, j)\n",
    "                if res[0] != \"0\":\n",
    "                    min_n = min(min_n, int(res))\n",
    "                    max_n = max(max_n, int(res))\n",
    "        return max_n - min_n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        def helper(x):\n",
    "            return all(i == x[0] for i in x)\n",
    "        if helper(str(num)):\n",
    "            n = len(str(num))\n",
    "            return int('8'*n)\n",
    "        choose = str(num)[0]\n",
    "        for i in str(num):\n",
    "            if i != '9':\n",
    "                choose = i\n",
    "                break\n",
    "        a = str(num).replace(choose, '9')\n",
    "        a = int(a)\n",
    "        if str(num)[0] == '1':\n",
    "            choose = str(num)[1]\n",
    "            for i in str(num)[1:]:\n",
    "                if i != '0' and i != '1':\n",
    "                    choose = i\n",
    "                    break\n",
    "            b = str(num).replace(choose, '0')\n",
    "            b = int(b)\n",
    "        else:\n",
    "            choose = str(num)[0]\n",
    "            b = str(num).replace(choose, '1')\n",
    "            b = int(b)\n",
    "        return a - b\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num = [int(x) for x in str(num)]\n",
    "        for pos_x in range(len(num)):\n",
    "            if num[pos_x] != 9:\n",
    "                break\n",
    "        min_ = None\n",
    "        for pos_y in range(len(num)):\n",
    "            if num[pos_y] != 1 and pos_y == 0:\n",
    "                min_ = 1\n",
    "                break\n",
    "            elif num[pos_y] != 0 and pos_y != 0 and num[pos_y] != 1:\n",
    "                min_ = 0\n",
    "                break\n",
    "        max_num = list(map(lambda x:x if x != num[pos_x] else 9, num))\n",
    "        if not min_ is None:\n",
    "            min_num = list(map(lambda x:x if x != num[pos_y] else min_, num))\n",
    "        else:\n",
    "            min_num = num\n",
    "        return int(''.join([str(n) for n in max_num])) - int(''.join([str(n) for n in min_num]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        l=list(str(num))\n",
    "        n=len(l)\n",
    "        target=-1\n",
    "        for i in range(n):\n",
    "            if l[i]!='9':\n",
    "                target=(l[i])\n",
    "                break\n",
    "        \n",
    "        \n",
    "        \n",
    "        maxx=num\n",
    "        \n",
    "        if target!=-1:\n",
    "            ll=[]\n",
    "            for i in l:\n",
    "                if i!=target:\n",
    "                    ll.append(i)\n",
    "                else:ll.append('9')\n",
    "            temp=int(''.join(ll))\n",
    "            maxx=max(maxx,temp)\n",
    "        \n",
    "        minn=inf\n",
    "        \n",
    "        if l==[l[0]]*n:\n",
    "            minn=int('1'*n)\n",
    "            \n",
    "        elif l[0]!='1':\n",
    "            target=l[0]\n",
    "            ll=[]\n",
    "            for i in l:\n",
    "                if i!=target:\n",
    "                    ll.append(i)\n",
    "                else:ll.append('1')\n",
    "            minn=int(''.join(ll))\n",
    "            \n",
    "            \n",
    "        else:\n",
    "            target=-1\n",
    "            for i in range(1,n):\n",
    "                if l[i]!=l[0] and l[i]!='0':\n",
    "                    target=l[i]\n",
    "                    break\n",
    "            if target==-1:\n",
    "                minn=num\n",
    "            else:\n",
    "                ll=[]\n",
    "                for i in l:\n",
    "                    if i!=target:\n",
    "                        ll.append(i)\n",
    "                    else:ll.append('0')\n",
    "                minn=int(''.join(ll))\n",
    "            \n",
    "        \n",
    "        \n",
    "        print(maxx,minn)\n",
    "        return maxx-minn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        mn = mx = num\n",
    "        # 最小值：如果第一个数不为1，替换它为1，否则替换第二个以后的第一个非0数为0，这个数还不能等于第一个数\n",
    "        # 如果第二个以后没有非0数，什么也不做\n",
    "        if num[0] != '1':\n",
    "            mn = num.replace(num[0], '1')\n",
    "        else:\n",
    "            for c in num[1:]:\n",
    "                if c != '0' and c != num[0]:\n",
    "                    mn = num.replace(c, '0')\n",
    "                    break\n",
    "        # 最大值:将第一个非9数替换为9\n",
    "        for c in num:\n",
    "            if c != '9':\n",
    "                mx = num.replace(c, '9')\n",
    "                break\n",
    "        return int(mx) - int(mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        strnum = list(str(num))\n",
    "        n = len(strnum)\n",
    "        #最小\n",
    "        rep = None\n",
    "        if(strnum[0] == '1'):\n",
    "            for i in range(1,n):\n",
    "                if(strnum[i] != '0' and strnum[i] != '1'):\n",
    "                    rep = strnum[i]\n",
    "                    break\n",
    "            if(rep == None):\n",
    "                minnum = strnum[:]\n",
    "            else:\n",
    "                minnum = ['1']\n",
    "                for i in range(1,n):\n",
    "                    if(strnum[i] == rep):\n",
    "                        minnum.append('0')\n",
    "                    else:\n",
    "                        minnum.append(strnum[i])\n",
    "        else:\n",
    "            rep = strnum[0]\n",
    "            minnum = ['1']\n",
    "            for i in range(1,n):\n",
    "                if(strnum[i] == rep):\n",
    "                    minnum.append('1')\n",
    "                else:\n",
    "                    minnum.append(strnum[i])\n",
    "        #最大\n",
    "        rep = None\n",
    "        for i in range(n):\n",
    "            if(strnum[i] != '9'):\n",
    "                rep = strnum[i]\n",
    "                break\n",
    "        if(rep == None):\n",
    "            maxnum = strnum[:]\n",
    "        else:\n",
    "            maxnum = []\n",
    "            for i in range(n):\n",
    "                if(strnum[i] == rep):\n",
    "                    maxnum.append('9')\n",
    "                else:\n",
    "                    maxnum.append(strnum[i])\n",
    "        \n",
    "        return int(''.join(maxnum)) - int(''.join(minnum))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int: \n",
    "        s = str(num)\n",
    "        n = len(s)\n",
    "        if s[0] == '1':\n",
    "            i = 1\n",
    "            while i < n and (s[i] == '0' or s[i] == '1'):\n",
    "                i += 1\n",
    "            if i < n:\n",
    "                mn = int(s.replace(s[i], '0'))\n",
    "            else:\n",
    "                mn = int(s)\n",
    "        else:\n",
    "            mn = int(s.replace(s[0], '1'))\n",
    "        if s[0] == '9':\n",
    "            i = 1\n",
    "            while i < n and s[i] == '9':\n",
    "                i += 1\n",
    "            if i < n:\n",
    "                mx = int(s.replace(s[i], '9'))\n",
    "            else:\n",
    "                mx = int(s)\n",
    "        else:\n",
    "            mx = int(s.replace(s[0], '9'))\n",
    "        return mx - mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        s_num = str(num)\n",
    "\n",
    "        if s_num[0] <= str(8):\n",
    "            max_num = int(s_num.replace(s_num[0], '9'))\n",
    "        else:\n",
    "            change_num = s_num[-1]\n",
    "            for i in range(len(s_num)):\n",
    "                if s_num[len(s_num)-i-1] != s_num[0]:\n",
    "                    change_num = s_num[len(s_num)-i-1]\n",
    "            max_num = int(s_num.replace(change_num, '9'))\n",
    "        \n",
    "        print(max_num)\n",
    "\n",
    "        if s_num[0] >= str(2):\n",
    "            min_num = int(s_num.replace(s_num[0], '1'))\n",
    "        else:\n",
    "            change_num = s_num[-1]\n",
    "            for i in range(len(s_num)-1):\n",
    "                if s_num[len(s_num)-i-1] != s_num[0] and s_num[len(s_num)-i-1] != '0':\n",
    "                    change_num = s_num[len(s_num)-i-1]\n",
    "    \n",
    "            if change_num != '1':\n",
    "                min_num = int(s_num.replace(change_num, '0'))\n",
    "            else:\n",
    "                min_num = int(s_num)\n",
    "        \n",
    "        print(min_num)\n",
    "\n",
    "        return max_num-min_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        str_num = str(num)\n",
    "        max_num = int(self.get_max(str_num))\n",
    "        min_num = int(self.get_min(str_num))\n",
    "        return max_num - min_num\n",
    "\n",
    "    def get_max(self, str_num):\n",
    "        temp_max_num = None\n",
    "        for i, s in enumerate(str_num):\n",
    "            if int(s) < 9:\n",
    "                temp_max_num = s\n",
    "                break\n",
    "        if temp_max_num:\n",
    "            str_num = str_num.replace(temp_max_num,'9')\n",
    "        return str_num\n",
    "\n",
    "    def get_min(self, str_num):\n",
    "        temp_min_num = None\n",
    "        min_num = None\n",
    "        for i, s in enumerate(str_num):\n",
    "            if int(s) > 1:\n",
    "                if i == 0:\n",
    "                    min_num = '1'\n",
    "                \n",
    "                else:\n",
    "                    if s == str_num[0]:\n",
    "                        min_num = '1'\n",
    "                    else:\n",
    "                        min_num = '0'\n",
    "                temp_min_num = s\n",
    "                break\n",
    "        if temp_min_num:\n",
    "            str_num = str_num.replace(temp_min_num,min_num)\n",
    "        return str_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        a = []\n",
    "        while num > 0:\n",
    "            a.append(num % 10)\n",
    "            num //= 10\n",
    "        \n",
    "        b, n = deepcopy(a), len(a)\n",
    "        if a[-1] != 1:\n",
    "            for i in range(n):\n",
    "                if a[i] == a[-1]:   a[i] = 1\n",
    "        else:\n",
    "            for i in range(n-1, -1, -1):\n",
    "                if a[i] not in (0, 1):\n",
    "                    for j in range(i+1):\n",
    "                        if a[j] == a[i]:    a[j] = 0\n",
    "                    break\n",
    "        A = 0\n",
    "        while a:    A = (A * 10) + a.pop()\n",
    "            \n",
    "        for i in range(n-1, -1, -1):\n",
    "            if b[i] != 9:\n",
    "                for j in range(i+1):\n",
    "                    if b[j] == b[i]:    b[j] = 9\n",
    "                break\n",
    "        B = 0\n",
    "        while b:    B = (B * 10) + b.pop()\n",
    "        return B - A\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        numstr = str(num)\n",
    "        results = 1\n",
    "        resultl = 9\n",
    "        flg1 = False\n",
    "        flg2 = False\n",
    "        a = 0\n",
    "        if numstr[0] != '1': \n",
    "            s = numstr[0]\n",
    "            flg1 = True\n",
    "            a = 1\n",
    "        if numstr[0] != '9':\n",
    "            l = numstr[0]\n",
    "            flg2 = True\n",
    "        for i in numstr[1:]:\n",
    "            if not flg1:\n",
    "                if i != '0' and i != '1':\n",
    "                    s = i\n",
    "                    flg1 = True\n",
    "                    results = results*10\n",
    "                else:\n",
    "                    results = results*10 + int(i)\n",
    "            elif i == s:\n",
    "                results = results * 10 + a\n",
    "            else:\n",
    "                results = results*10 +int(i)\n",
    "                \n",
    "            if not flg2:\n",
    "                if i != '9':\n",
    "                    l = i\n",
    "                    flg2 = True\n",
    "                    resultl = resultl*10 + 9\n",
    "                else:\n",
    "                    resultl = resultl*10 + 9\n",
    "            elif i == l:\n",
    "                resultl = resultl * 10 + 9\n",
    "            else:\n",
    "                resultl = resultl*10 +int(i)\n",
    "        # print(results)\n",
    "        return resultl - results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        n = len(str(num))\n",
    "        num = list(str(num))\n",
    "        #1.把第一个非9的数变成9\n",
    "        not9 = -1\n",
    "        not0 = -1\n",
    "        not01 = -1\n",
    "        for i in range(n):\n",
    "            if not9 == -1 and num[i] != \"9\":\n",
    "                not9 = i\n",
    "\n",
    "            if not0 == -1 and num[i] != \"0\" and i != 0:\n",
    "                not0 = i\n",
    "\n",
    "            if not01 == -1 and num[i] != \"0\" and num[i] != \"1\":\n",
    "                not01 = i    \n",
    "\n",
    "        def change(pre, tar):\n",
    "            new = num[:]\n",
    "            for i in range(n):\n",
    "                if new[i] == pre:\n",
    "                    new[i] = tar\n",
    "            return int(''.join(new))\n",
    "\n",
    "        if not9 != -1:\n",
    "            x = num[not9]\n",
    "            maxNum = change(x, \"9\")\n",
    "        else:\n",
    "            maxNum = int(''.join(num))\n",
    "\n",
    "        #能把num[0]变成1\n",
    "        if num[0] > \"1\":\n",
    "            x = num[0]\n",
    "            minNum = change(x, \"1\")\n",
    "            \n",
    "        #能把num[not0]变成0    \n",
    "        elif num[not0] != \"1\":\n",
    "            x = num[not0]\n",
    "            minNum = change(x, \"0\")       \n",
    "\n",
    "        #把第一个非0非1变成0\n",
    "        else:\n",
    "            if not01 != -1:\n",
    "                x = num[not01] \n",
    "                minNum = change(x, \"0\")\n",
    "            else:\n",
    "                minNum = int(''.join(num))    \n",
    "\n",
    "        return maxNum - minNum\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        l_min, l_max = '999999999', ''\n",
    "        for i in '0123456789':\n",
    "            if s[0]!=i: l_min = min(s.replace(i, '0'), l_min)\n",
    "            l_min = min(s.replace(i, '1'), l_min)\n",
    "            l_max = max(s.replace(i, '9'), l_max)\n",
    "        return int(l_max)-int(l_min)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        num1=num2=num=str(num)\n",
    "        for char in num:\n",
    "            if char!='9':\n",
    "                num1=num.replace(char,'9')\n",
    "                break\n",
    "        for index,char in enumerate(num):\n",
    "            if char!='1' and index==0:\n",
    "                num2=num.replace(char,'1')\n",
    "                break\n",
    "            if char!='0' and index!=0 and char!=num[0]:\n",
    "                num2=num.replace(char,'0')\n",
    "                break\n",
    "        # print(num1,num2)\n",
    "        return int(num1)-int(num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # # 贪心\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "\n",
    "        # 最大值为替换9\n",
    "        ceil = num\n",
    "        for w in s:\n",
    "            if w != \"9\":\n",
    "                ceil = int(s.replace(w, \"9\"))\n",
    "                break\n",
    "        \n",
    "        # 最小值为替换0或者替换1\n",
    "        floor = num\n",
    "        if s[0] == \"1\":\n",
    "            for w in s[1:]:\n",
    "                if w > \"1\":\n",
    "                    floor = int(s.replace(w, \"0\"))\n",
    "                    break\n",
    "        else:\n",
    "            floor = int(s.replace(s[0], \"1\"))\n",
    "\n",
    "        return ceil - floor\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxDiff(self, num: int) -> int:\n",
    "        def change_max(num):\n",
    "            s = str(num)\n",
    "            n = len(s)\n",
    "            if n == 1:\n",
    "                return 9\n",
    "            idx = 0\n",
    "            while idx < n and s[idx] == '9':\n",
    "                idx += 1\n",
    "            if idx == n:\n",
    "                return num\n",
    "            x = s[idx]\n",
    "            res = s[:idx]\n",
    "            for i in range(idx, n):\n",
    "                if s[i] == x:\n",
    "                    res = res + '9'\n",
    "                else:\n",
    "                    res = res + s[i]\n",
    "            return int(res)\n",
    "        def change_min(num):\n",
    "            s = str(num)\n",
    "            n = len(s)\n",
    "            if n == 1:\n",
    "                return 1\n",
    "            if s[0] != '1':\n",
    "                x = s[0]\n",
    "                res = ''\n",
    "                for i in range(n):\n",
    "                    if s[i] == x:\n",
    "                        res += '1'\n",
    "                    else:\n",
    "                        res += s[i]\n",
    "                return int(res)\n",
    "            idx = 0\n",
    "            while idx < n and (s[idx] == '1' or s[idx] == '0'):\n",
    "                idx += 1\n",
    "            \n",
    "            if idx == n:\n",
    "                return num\n",
    "            x = s[idx]\n",
    "            res = s[:idx]\n",
    "            for i in range(idx, n):\n",
    "                if s[i] == x:\n",
    "                    res = res + '0'\n",
    "                else:\n",
    "                    res = res + s[i]\n",
    "            return int(res)\n",
    "        return change_max(num) - change_min(num)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
